<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools>
        <template v-slot:after>
          <el-button type="primary" size="small" @click="addPermission('0',1)">添加权限</el-button>
        </template>
      </page-tools>
      <el-card>
        <el-table :data="list" border="" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template slot-scope="{ row }">
              <el-button v-if="row.type === 1" type="text" @click="addPermission(row.id, 2)">添加</el-button>
              <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
              <el-button type="text" @click="delPermission(row.id)"> 删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <addPermission ref="perForm" :show-dialog.sync="showDialog" @show="getPermissionList" />
    </div>
  </div>
</template>

<script>
import { getPermissionList, delPermission } from '@/api/permisson'
import { listToTreeData } from '@/utils'
import addPermission from './components/add-permission'
export default {
  components: {
    addPermission
  },
  data() {
    return {
      list: [],
      showDialog: false
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async  getPermissionList() {
      this.list = listToTreeData(await getPermissionList(), '0')
      console.log(this.list)
    },
    addPermission(pid, type) {
      // console.log('pid', pid)
      // console.log('type', type)
      this.$refs.perForm.addPermission(pid, type)
      this.showDialog = true
    },
    async  delPermission(id) {
      await delPermission(id)
      this.$message.success('操作成功')
      this.getPermissionList()
    }
  }
}
</script>

<style>

</style>
